package cn.clx.org.service.impl;

import cn.clx.basic.exception.BusinessException;
import cn.clx.basic.service.impl.BaseServiceImpl;
import cn.clx.basic.util.BaiduAuditUtils;
import cn.clx.org.domain.Employee;
import cn.clx.org.domain.Shop;
import cn.clx.org.mapper.EmployeeMapper;
import cn.clx.org.mapper.ShopMapper;
import cn.clx.org.service.IEmployeeService;
import cn.clx.org.service.IShopService;
import cn.clx.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author clx
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 店铺入驻业务+校验
     * @param shop
     * @throws BusinessException
     */
    @Override
    public void settlement(Shop shop) throws BusinessException {
        Employee admin = shop.getAdmin();
        //要进行校验(前端虽然帮我们做了校验但是，我们还是要在进行一下 防止别人发postman来攻击我们)
        if(StringUtils.isEmpty(shop.getName())||
                StringUtils.isEmpty(shop.getAddress())||
                StringUtils.isEmpty(admin.getUsername())){
            throw new BusinessException("请重新输入，店铺、店铺地址、店长不能为空");
        }
        // 名词合法校验  调用百度ai的接口来校验的  在java代码中发送了http请求   TextCensor  校验是否合法
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("骚年你在玩火");
        }
        //判断该店铺是否被注册了
        Shop shoptemp =shopMapper.loadByName(shop.getName());
        if (shoptemp!=null){
            throw new BusinessException("该店铺已经被注册了");
        }
        //判断注册的员工是否存在
        Employee employee = employeeMapper.loadByusername(admin.getUsername());
        //如存在且有管理的店铺
        if(employee!=null && employee.getShop_id()!=null){
            throw new BusinessException("该账号已经被注册了，请更换用户");
        }
        //判断员工是否注册了
        if(employee==null){
            employeeService.save(admin);
            employee=admin;
        }
        //经过上面的代码 员工必定有对象
        //设置shop表中的管理员
        shop.setAdminId(employee.getId());
        shopMapper.save(shop);
        //给存在的员工进行shop_id进行赋值
        employee.setShop_id(shop.getId());
        employeeService.update(employee);


        //判断注册人是否已经被注册了
     /*   if(employee!=null){
            //已经注册了
            shop.setAdminId(employee.getId());
            shopMapper.save(shop);
            employee.setShop_id(shop.getId());
            employeeMapper.update(employee);
        }else {
            //没有注册
            shopMapper.save(shop);
            admin.setShop_id(shop.getId());
            employeeMapper.save(admin);
            shop.setAdminId(admin.getId());
            shopMapper.update(shop);
        }*/

    }

    /**
     * 批量新增(报表导入)
     * @param list
     */
    @Override
    public void saveList(List<Shop> list) {
        shopMapper.saveList(list);
    }

    /**
     * 驳回重提交
     * @param shop
     */
    @Override
    public void turnDown(Shop shop) {
        shop.setState(0);
        shopMapper.update(shop);
    }

    /**
     * echarts 报表
     * @return
     */
    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }
}
